package com.swipemaster.levels;

import java.util.ArrayList;

import javax.microedition.khronos.opengles.GL10;

import com.swipemaster.common.Coords;
import com.swipemaster.elements.Element;

/**
 * Abstract level class. We should derive from this class.
 */
public class Level
{
	/** Enemies' elements. */
	protected ArrayList<Element> enemies = new ArrayList<Element>();
	/** Element steered by player. */
	protected Element player;
	/** When player started to play level. */
	protected long startTime;
	
	public Level(Element player)
	{
		this.player = player;
	}

	/**
	 * @return True, if player is colliding with any enemy.
	 */
	public boolean checkCollisions()
	{
		for (Element enemy : enemies)
		{
			if (enemy.isCollisionWith(player))
			{
				return true;
			}
		}
		return false;
	}

	/**
	 * Periodically called to move all the enemies.
	 */
	public void moveEnemies()
	{
		for (Element enemy : enemies)
		{
			enemy.move();
		}
	}

	/**
	 * Actions performed on start of level.
	 * TODO: maybe move this to constructor
	 */
	public void startGame()
	{
		startTime = System.currentTimeMillis();
	}

	/**
	 * Adds an enemy element to the level.
	 */
	public void addEnemy(Element enemy)
	{
		enemies.add(enemy);
	}

	/**
	 * Draw everything, enemies and player.
	 */
	public void draw(GL10 gl)
	{
		player.draw(gl);
		for (Element enemy : enemies)
		{
			enemy.draw(gl);
		}
	}
	
	/**
	 * Should be abstract, but Gson has problems with polymorphism.
	 * 
	 * @return True, if user is already a winner.
	 */
	boolean isWin() 
	{
		throw new RuntimeException("You can't call abtract methods.");
	}
	
	public void setPlayerPosition(Coords coords)
	{
		player.setCoords(coords);
	}
}
